﻿using HandyControl.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 模拟器远程控制
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static MainWindowViewModel ViewModel { get; set; } = new MainWindowViewModel();
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = ViewModel;
        }

        private void TreeViewItem_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)

        {

            e.Handled = true;

        }
        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {


        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            //if (HandyControl.Controls.MessageBox.Show($"确定关闭系统?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
            //{
            //    SystemManage.SaveConfig();
            //    System.Environment.Exit(0);
            //}

            if (HandyControl.Controls.MessageBox.Show(
                new HandyControl.Data.MessageBoxInfo()
                {
                    Message = "确认关闭系统？",
                    Caption = "提示",
                    Button = MessageBoxButton.YesNo,
                    IconBrushKey = ResourceToken.AccentBrush,
                    IconKey = ResourceToken.WarningGeometry,
                    StyleKey = "MessageBoxCustom1"
                }
                ) == MessageBoxResult.Yes)
            {
                System.Environment.Exit(0);
            }
        }


        private void MinimizeButton_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void MaximizeButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.WindowState != WindowState.Maximized)
            {
                this.WindowState = WindowState.Maximized;
            }
            else
            {
                this.WindowState = WindowState.Normal;
            }

        }

        private void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.WindowState != WindowState.Maximized)
            {
                this.WindowState = WindowState.Maximized;
            }
            else
            {
                this.WindowState = WindowState.Normal;
            }
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            textBox.ScrollToEnd();
        }

        private bool isDragging = false;
        private Point startPoint;

        private void Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                isDragging = true;
                Border button = sender as Border;
                Mouse.Capture((IInputElement)sender);

                startPoint = e.GetPosition(bd);
            }
        }

        private void Button_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                Border button = sender as Border;
                Point position = e.GetPosition(canvas);
                //Canvas.SetLeft(button, position.X);
                //Canvas.SetTop(button, position.Y);
                Canvas.SetLeft(bd, position.X - startPoint.X);
                Canvas.SetTop(bd, position.Y - startPoint.Y);
            }
        }

        private void Button_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Released)
            {
                isDragging = false;
                Mouse.Capture(null);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("点击了");
        }

        [DllImport("User32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
        const int KEYEVENTF_KEYDOWN = 0;
        const int KEYEVENTF_KEYUP = 0x2;
        private void Button_Click_1(object sender, RoutedEventArgs e)
       {
            System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
            string zf = button.Content.ToString();
            switch (zf)
            {
                case "1":
                    keybd_event((byte)System.Windows.Forms.Keys.D1, 0, KEYEVENTF_KEYDOWN, 0);
                    keybd_event((byte)System.Windows.Forms.Keys.D1, 0, KEYEVENTF_KEYUP, 0);
                    break;
                case "2":
                    keybd_event((byte)System.Windows.Forms.Keys.D2, 0, KEYEVENTF_KEYDOWN, 0);
                    keybd_event((byte)System.Windows.Forms.Keys.D2, 0, KEYEVENTF_KEYUP, 0);
                    break;
                case "3":
                    keybd_event((byte)System.Windows.Forms.Keys.D3, 0, KEYEVENTF_KEYDOWN, 0);
                    keybd_event((byte)System.Windows.Forms.Keys.D3, 0, KEYEVENTF_KEYUP, 0);
                    break;
                case "4":
                    keybd_event((byte)System.Windows.Forms.Keys.D4, 0, KEYEVENTF_KEYDOWN, 0);
                    keybd_event((byte)System.Windows.Forms.Keys.D4, 0, KEYEVENTF_KEYUP, 0);
                    break;
                case "5":
                    keybd_event((byte)System.Windows.Forms.Keys.D5, 0, KEYEVENTF_KEYDOWN, 0);
                    keybd_event((byte)System.Windows.Forms.Keys.D5, 0, KEYEVENTF_KEYUP, 0);
                    break;
                case "6":
                    keybd_event((byte)System.Windows.Forms.Keys.D6, 0, KEYEVENTF_KEYDOWN, 0);
                    keybd_event((byte)System.Windows.Forms.Keys.D6, 0, KEYEVENTF_KEYUP, 0);
                    break;
                case "7":
                    keybd_event((byte)System.Windows.Forms.Keys.D7, 0, KEYEVENTF_KEYDOWN, 0);
                    keybd_event((byte)System.Windows.Forms.Keys.D7, 0, KEYEVENTF_KEYUP, 0);
                    break;
                case "8":
                    keybd_event((byte)System.Windows.Forms.Keys.D8, 0, KEYEVENTF_KEYDOWN, 0);
                    keybd_event((byte)System.Windows.Forms.Keys.D8, 0, KEYEVENTF_KEYUP, 0);
                    break;
                case "9":
                    keybd_event((byte)System.Windows.Forms.Keys.D9, 0, KEYEVENTF_KEYDOWN, 0);
                    keybd_event((byte)System.Windows.Forms.Keys.D9, 0, KEYEVENTF_KEYUP, 0);
                    break;
                case "0":
                    keybd_event((byte)System.Windows.Forms.Keys.D0, 0, KEYEVENTF_KEYDOWN, 0);
                    keybd_event((byte)System.Windows.Forms.Keys.D0, 0, KEYEVENTF_KEYUP, 0);
                    break;
                case "-":
                    keybd_event((byte)System.Windows.Forms.Keys.OemMinus, 0, KEYEVENTF_KEYDOWN, 0);
                    keybd_event((byte)System.Windows.Forms.Keys.OemMinus, 0, KEYEVENTF_KEYUP, 0);
                    break;
                case ".":
                    keybd_event((byte)System.Windows.Forms.Keys.Decimal, 0, KEYEVENTF_KEYDOWN, 0);
                    keybd_event((byte)System.Windows.Forms.Keys.Decimal, 0, KEYEVENTF_KEYUP, 0);
                    break;
                case "⬅":
                    keybd_event((byte)System.Windows.Forms.Keys.Back, 0, KEYEVENTF_KEYDOWN, 0);
                    keybd_event((byte)System.Windows.Forms.Keys.Back, 0, KEYEVENTF_KEYUP, 0);
                    break;
                default:
                    break;
            }
        }
    }
}
